<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

class M_user_management extends BZ_Model
{
/**
 * @section biskitz User Management Model
 */

	/**
	 * @function _menu_parent_get()
	 * @description get the parent of a menu
	 */
	function _menu_parent_get($prm_dbase = 'db_default', $prm_user_id, $prm_module_id, $prm_id, $prm_access_type)
	{
		$use_db = $this->{$prm_dbase};

		$qry = $use_db->get_where
			(
				'biskitz_menus'
				, array('modules__id' => $prm_module_id, 'id' => $prm_id)
			);

		if ($qry->num_rows() > 0)
		{
			$sql = 'insert ignore into bz_biskitz_user_access values(?, ?, ?, ?)';
			$data = array($prm_user_id, $prm_module_id, $prm_id, $prm_access_type);

			$use_db->query($sql, $data);

			if ($qry->row()->parent_id != 0 && $prm_access_type != 'favorite')
			{
				$this->_menu_parent_get($prm_dbase, $prm_user_id, $prm_module_id, $qry->row()->parent_id, 'view');
			}
		}
	}


	/**
	 * @function access_right_set()
	 * @description set the user access rights
	 */
	function access_right_set($prm_dbase = 'db_default', $prm_user_id = '', $prm_user_access = array(), $prm_user_access_type = 'access_rights')
	{
		$use_db = $this->{$prm_dbase};

		$prm_user_access_type == 'favorite' ? $sql_favorite = array('type' => 'favorite') : $sql_favorite = array();
		$use_db->delete
			(
				'biskitz_user_access'
				, array_merge(array('biskitz_user__id' => $prm_user_id), $sql_favorite)
			);

		if (is_array($prm_user_access))
		{
			foreach ($prm_user_access as $val)
			{
				$access_rights = explode(';', $val);
				$this->_menu_parent_get($prm_dbase, $prm_user_id, $access_rights[0], $access_rights[1], $access_rights[2]);
			}
		}
	}


	/**
	 * @function access_right_get()
	 * @description get the user access rights
	 */
	function access_right_get($prm_dbase = 'db_default', $prm_user_id, $prm_object_return_type = 'checkbox', $prm_favorite_use = FALSE)
	{
		$return_value = array();

		if (trim($prm_user_id) != '')
		{
			$use_db = $this->{$prm_dbase};
			$tbl_attr = array
				(
					'TABLE' => array
						(
							'NAME' => 'biskitz_user_access'
						)
				);

			$qry = $use_db->get_where
				(
					$tbl_attr['TABLE']['NAME']
					, array_merge
						(
							array('biskitz_user__id' => $prm_user_id)
							, ($prm_favorite_use == TRUE) ? array('type' => 'favorite') : array('type !=' => 'favorite')
						)
				);

			if ($qry->num_rows() > 0)
			{
				foreach ($qry->result() as $rows)
				{
					$return_data[] = $rows->modules__id.';'.$rows->menus__id.';'.$rows->type;
				}

				$return_value[] = array($prm_object_return_type, $return_data);
			}
		}


		return $return_value;
	}



}

/** End of file m_user_management.php */
/** Location: ./biskitz_mpc/biskitz/models/admin/biskitz/administration/m_user_management.php */